package com.entor.hqj.main;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.entor.hqj.login.LoginActivity;
import com.entor.hqj.login.R;
import com.entor.hqj.login.RegisterActivity;
import com.entor.hqj.model.User;
import com.entor.hqj.model.voter;
import com.entor.hqj.util.HttpPostUtils;
import com.entor.hqj.util.MoreTextView;
import com.entor.hqj.util.RadioGroupEx;
import com.entor.hqj.util.StringUtil;
import com.entor.hqj.util.TitleBuilder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class NotFinishVoterActivity extends AppCompatActivity {
    private MoreTextView title = null;
    private MoreTextView content = null;
    private MoreTextView time = null;
    private MoreTextView type = null;
    private RadioGroupEx rg;
    //private MoreTextView voter = null;
    private boolean flag = true;
    private String s = "";
    private Date t2;
    private Handler handler=null;
    private Long voters;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_not_finish_voter);
        title = (MoreTextView)findViewById(R.id.et_title) ;
        content = (MoreTextView)findViewById(R.id.et_content);
        time = (MoreTextView)findViewById(R.id.et_time) ;
        type = (MoreTextView)findViewById(R.id.et_type) ;
        rg = (RadioGroupEx)findViewById(R.id.rg);
       // voter = (MoreTextView)findViewById(R.id.et_voter);
        handler = new Handler();
        initTitle();
        getData();

    }

    public void initTitle(){
        new TitleBuilder(this).setLeftText("返回").setMidTitleText("未投票")
                .setRightText("确定")
                .setLeftImgOrTextListener(new leftImgOrTextListener())
                .setRightImgOrTextListener(new rightImgOrTextListener());
    }

    class leftImgOrTextListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            finish();
        }
    }

    class rightImgOrTextListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            if(voters==null){
                Toast.makeText(NotFinishVoterActivity.this, "请选择候选人", Toast.LENGTH_SHORT).show();
            }
            Intent intent = getIntent();
            Long id = intent.getLongExtra("vi_id", 0);
            final String data = "id="+id+"&voters="+voters;
            new Thread(){

                @Override
                public void run(){
                    String response = HttpPostUtils.doPostRequest("voter/saveVoter",data);
                    JSONObject resultJson =
                            JSON.parseObject(response);
                    if(resultJson!=null) {
                        String msg = resultJson.getString("msg");
                        boolean sus = resultJson.getBoolean("success");
                        //Toast.makeText(NotFinishVoterActivity.this, msg, Toast.LENGTH_SHORT).show();
                        if(sus){
                            Intent intent = new Intent(NotFinishVoterActivity.this,TablayoutActivity.class);
                            startActivity(intent);
                        }
                    }
                }
            }.start();

        }
    }

    public void getData(){
        try {
            Intent intent = getIntent();
            Integer id = intent.getIntExtra("id", 0);
            Long vi_id = intent.getLongExtra("vi_id", 0);
            final String notfinish = "id=" + id + "&vi_id=" + vi_id;
            new Thread() {
                @Override
                public void run() {
                    String response =
                            HttpPostUtils.doPostRequest("voter/gotovoterByMobile", notfinish);
                    Message message = Message.obtain();
                    message.obj = response;
                    String result = (String) message.obj;
                    if (result != null) {
                        voter v = JSON.parseObject(result, voter.class);
                        title.setText(v.getTitle());
                        content.setText(v.getContent());
                        t2 = v.getEndtime();
                        time.setText(StringUtil.getTimeFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), t2));
                        type.setText("投票类型:一人一票");
                    }
                }
            }.start();
            //Thread.sleep(1000);
            createRadioButton();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onStop(){
        flag = false;
        super.onStop();
    }

    Runnable  runnableUi = new  Runnable(){
        @Override
        public void run() {
            while (flag) {//这是个死循环,需要在activity消失时,把flag设为false,结束循环
                try {
                    time.setText(StringUtil.getTimeFormat(
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),
                            t2));
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };


    public void createRadioButton(){
        Intent intent = getIntent();
        String candidate = intent.getStringExtra("candidate");
        final  String data = "candidate="+candidate;
        new Thread(){
            @Override
            public void run(){
                String response =
                        HttpPostUtils.doPostRequest("voter/findCandidate",data);
                Message message = Message.obtain();
                message.obj = response;
                String result = (String) message.obj;
                if(result!=null){
                    List<User> list = JSON.parseArray(result,User.class);
                    for (User u:list){
                        final User user = u;
                        final RadioButton rb = (RadioButton) LayoutInflater.from(NotFinishVoterActivity.this).inflate(R.layout.radiobutton,null);
                        rb.setText(u.getName());

                        rb.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(NotFinishVoterActivity.this,
                                        user.getName(),Toast.LENGTH_SHORT).show();
                                voters = user.getId();
                            }
                        });
                        rg.addView(rb);
                    }
                }
            }
        }.start();

    }
}
